package com.wyh6635.rate.redis;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;

import redis.clients.jedis.Jedis;

import com.google.common.collect.Lists;
import com.google.common.io.Files;


public class RedisLuaDemo {

	public static void main(String[] args) throws Exception {
		for ( int i = 0; i < 5; i ++ ) {
			Thread t = new Thread(new Runnable(){
			
				@Override
				public void run() {
					try {
						acquire();
					} catch (Exception e) {
						System.out.println(e);
					}
				}
			});
			
			t.start();
		}
	}
	
	
	private static boolean acquire() throws Exception {
		String luaScript = Files.toString(new File("F:\\OpenSource\\rate-demo\\src\\main\\java\\com\\wyh6635\\rate\\redis\\limit.lua"), Charset.defaultCharset());
		Jedis jedis = new Jedis("172.30.0.180", 6380);
		String key = "ip:test160";
		String limit = "2"; //限流大小
		boolean flag = (Long)jedis.eval(luaScript,Lists.newArrayList(key), Lists.newArrayList(limit)) == 1;
		System.out.println(Thread.currentThread().getName() + ":" + flag);
		return true;
	}
}

	